﻿namespace BLL
{
    using DAL;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SlaController
    {
        public DataTable GetSLA(int idSLA)
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand qrySLA = new SqlCommand("SELECT idSLA, SLA, Prioridade, TempoResposta, TempoSolucao FROM SLA WHERE idSLA = @idSLA");
            qrySLA.Parameters.AddWithValue("@idSLA", idSLA);
            return connMgr.doReader(qrySLA);
        }

        public string GetSLAInfo(int idSLA)
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand qryX = new SqlCommand("SELECT [SLA] + ', ' + CONVERT(varchar,[TempoResposta]) + ' dias, ' + CONVERT(varchar,[TempoSolucao]) + ', dias' as Inline FROM [dbo].[SLA] WHERE idSLA = @idSLA");
            qryX.Parameters.AddWithValue("@idSLA", idSLA);
            DataRow dr = connMgr.doReader(qryX).Rows[0];
            return dr[0].ToString();
        }

        public DataTable GetSLAs()
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand qrySLAs = new SqlCommand("SELECT idSLA, SLA, Prioridade, TempoResposta, TempoSolucao FROM SLA");
            return connMgr.doReader(qrySLAs);
        }

        public DataTable GetSLAsInline()
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand qryX = new SqlCommand("SELECT idSLA, [SLA] + ', ' + CONVERT(varchar,[TempoResposta]) + ' dias, ' + CONVERT(varchar,[TempoSolucao]) + ', dias' as Inline FROM [dbo].[SLA] ORDER BY idSLA");
            return connMgr.doReader(qryX);
        }

        public void newSLA(string SLA, string Prioridade, int TempoResposta, int TempoSolucao)
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand qryX = new SqlCommand("INSERT INTO [SLA] (\r\n                                                                  [SLA]\r\n                                                                 ,[Prioridade]\r\n                                                                 ,[TempoResposta]\r\n                                                                 ,[TempoSolucao])\r\n                                                        VALUES\r\n                                                                 (@SLA,@Prioridade,@TempoResposta,@TempoSolucao)");
            qryX.Parameters.AddWithValue("@SLA", SLA);
            qryX.Parameters.AddWithValue("@Prioridade", Prioridade);
            qryX.Parameters.AddWithValue("@TempoResposta", TempoResposta);
            qryX.Parameters.AddWithValue("@TempoSolucao", TempoSolucao);
            connMgr.doCommand(qryX);
        }

        public void UpdSLA(int idSLA, string SLA, string Prioridade, int TempoResposta, int TempoSolucao)
        {
            ConnectionManager connMgr = new ConnectionManager();
            SqlCommand updSLA = new SqlCommand("UPDATE SLA SET Prioridade = @Prioridade, TempoResposta = @TempoResposta, TempoSolucao = @TempoSolucao, SLA = @SLA WHERE idSLA = @idSLA");
            updSLA.Parameters.AddWithValue("@idSLA", idSLA);
            updSLA.Parameters.AddWithValue("@Prioridade", Prioridade);
            updSLA.Parameters.AddWithValue("@TempoResposta", TempoResposta);
            updSLA.Parameters.AddWithValue("@TempoSolucao", TempoSolucao);
            updSLA.Parameters.AddWithValue("@SLA", SLA);
            connMgr.doCommand(updSLA);
        }
    }
}

